/**
 * Created by Yu Jin Xian on 2016/12/21.
 */
function creatingObjects() {
    var person = new Object();
    person.name = 'Nicholas';
    person.age = 29;
    person.job = 'Software Engineer';

    person.sayName = function () {
        alert(this.name);
    };

    function or() {
        var person = {
            name: 'Nicholas',
            age: 29,
            job: 'Software Engineer',

            sayName: function () {
                alert(this.name);
            }
        }
    }

    function propertyTpye() {
        function dataProperties() {
            var person = {};
            Object.defineProperty(person, 'name', {
                writeable: false,
                value: 'Nicholas'
            });

            alert(person.name);                     // "Nicholas"
            person.name = 'Greg';
            alert(person.name);                     // "Nicholas"
        }

        function configurable() {
            var person = {};
            Object.defineProperty(person, 'name', {
                configurable: false,
                value: 'Nicholas'
            });

            alert(person.name);                     // "Nicholas"
            delete person.name;
            alert(person.name);                     // "Nicholas"
        }

        function error() {
            var person = {};
            Object.defineProperty(person, 'name', {
                configurable: false,
                value: 'Nicholas'
            });

            Object.defineProperty(person, 'name', {
                configurable: true,
                value: 'Nicholas'
            });                                     // 抛出错误
        }

        function accessorProperties() {
            var book = {
                _year: 2004,
                edition: 1
            };

            Object.defineProperty(book, 'year', {
                get: function () {
                    return this._year;
                },
                set: function (value) {
                    if (value > 2004) {
                        this._year = value;
                        this.edition += value - 2004;
                    }
                }
            });

            book.year = 2005;
            alert(book.edition);                    // 2

            function oldMedthods() {
                var book = {
                    _year: 2004,
                    edition: 1
                };

                // 定义访问器的旧有方法
                book.__defineGetter__('year', function () {
                    return this._year;
                });

                book.__defineSetter__('year', function () {
                    if (value > 2004) {
                        this._year = value;
                        this.edition += value - 2004;
                    }
                });

                book.year = 2005;
                alert(book.edition);                // 2
            }
        }
    }
}

function defineProperties() {
    var book = {};

    Object.defineProperties(book, {
        _yaer: {writable: true, value: 2004},
        edition: {writeable: tre, value: 1},
        year: {
            get: function () {
                return this._year;
            },
            set: function (value) {
                if (value > 2004) {
                    this._year = value;
                    this.edition += value - 2004;
                }
            }
        }
    });
}

function getOwnPropertyDescriptor() {
    var book = {};

    Object.defineProperties(book, {
        _yaer: {writable: true, value: 2004},
        edition: {writeable: tre, value: 1},
        year: {
            get: function () {
                return this._year;
            },
            set: function (value) {
                if (value > 2004) {
                    this._year = value;
                    this.edition += value - 2004;
                }
            }
        }
    });

    var descriptor = Object.getOwnPropertyDescriptor(book, '_year');
    alert(descriptor.value);                        // 2004
    alert(descriptor.confgurable);                  // false
    alert(typeof  descriptor.get);                  // "undefinded"

    function getPropertyDescriptor() {
        var descriptor = Object.getOwnPropertyDescriptor(book, 'year');
        alert(descriptor.value);                        // "undefinded"
        alert(descriptor.confgurable);                  // false
        alert(typeof  descriptor.get);                  // "function"
    }

}